$(function () {
    var layer = layui.layer
    var form = layui.form
    function loadSwiperList() {
        $.ajax({
            url: "admin/swipers",
            success: function (res) {
                var tage = template('table-tpl', res)
                $('.layui-table tbody').html(tage)
            }

        })
    }

    loadSwiperList()
    // 添加

    $(".layui-table tbody").on('click', '.layui-badge', function (e) {
        let status = $(e.target).data('status')
        let id = $(e.target).data('id')

        $.ajax({
            url: "admin/swipers/" + id,
            type: 'PUT',
            data: {
                status: status
            },
            success: function (res) {
                if (res.status === 0) {
                    layer.msg(res.message)
                    loadSwiperList()
                }
            }
        })
    })

    // 删除
    $(".layui-table tbody").on('click', '.layui-btn', function (e) {
        let id = $(e.target).data("id")
        layer.confirm('你他妈的，要删除？？？？？?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                url: "admin/swipers/" + id,
                type: 'DELETE',
                success: function (res) {
                    if (res.status === 0) {
                        layer.msg(res.message)
                        loadSwiperList()
                    }
                }

            })

            layer.close(index);
        });

    })

    // 上传
    $('.layui-card').on('click', '#test1', function () {
        $('#myfile').click()
        console.log(12);
    })

    //   监听
    $('body').on('change', '#myfile', function (e) {
        console.log(111)
        let files = e.target.files
        var fd = new FormData()
        $.each(files, function (index, item) {
            fd.append('swipers', item)
        })
        $.ajax({
            type: 'post',
            url: 'admin/swipers',
            data: fd,
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.status === 0) {
                    layer.msg(res.message)
                    loadSwiperList()
                }
            }
        })
    })
})
